<?php

function cmd_all($_POST)
{
 $sql = new db();
 for ($i = 0; $i < sizeof($_POST['Cmd_Id']); $i++)
 {
  $t = time();
  $req = "INSERT INTO commande VALUES(NULL, NOW(), '".$_SESSION['commande'][$_POST['Cmd_Id'][$i]]['Date']."', '".$_SESSION['commande'][$_POST['Cmd_Id'][$i]]['Date']."', 'N', 'p', '0', 'NULL', '".$_SESSION['login']."', '".$_SESSION['login']."', '".$t."', '".$_POST['Cmd_FMP'][$i]."')";
  $sql->query($req);
  $req = "SELECT MAX(Id_Commande) AS Id_Cmd FROM commande";
  $sql->query($req);
  $id = $sql->fetch_array();
  $req = "INSERT INTO ligne_commande VALUES(NULL, '1', '".$_POST['Cmd_Q'][$i]."', '".$id['Id_Cmd']."', '".$_POST['Cmd_Id'][$i]."')";
  $sql->query($req);
  $req = "INSERT INTO preparation VALUES (NULL, NOW(), '0000-00-00', '0', '', '0000-00-00', '0000-00-00', '".$id['Id_Cmd']."')";
  $sql->query($req);
  }
   unset($_SESSION['commande']);
} 

function view_all($_POST)
{
unset($_SESSION['commande']);
$sql = new db();
$req = "SELECT s.* FROM stocker AS s WHERE s.GLN_Entrepot=".$_SESSION['login']; // SELECT s.*, p.GLN_FMP FROM stocker AS s, produit AS p, colis_type AS ct WHERE s.GLN_Entrepot=$_SESSION['login'] AND s.Id_Colis=ct.Id_Colis AND ct.GTIN_Produit=p.GTIN_Produit
$sql->query($req);
$stock = array();
while ($tmp = $sql->fetch_array())
 {
  $stock[] = $tmp;
 }
foreach ($stock AS $info)
 {
  $req = "SELECT SUM(lc.quantite) AS Quantite FROM ligne_commande AS lc, commande AS c, preparation AS p WHERE c.GLN_Entrepot=".$_SESSION['login']." AND c.Etat='p' AND c.Id_Commande=lc.Id_Commande AND c.Id_Commande=p.Id_Commande AND (p.Date_Fin BETWEEN '".$_POST['GestStock_DateD']."' AND'".$_POST['GestStock_DateF']."') AND lc.Id_colis='".$info['Id_colis']."'";
  $sql->query($req);
  if ($sql->num_rows() > 0)
   $data = $sql->fetch_array();
  else
   $data['Quantite'] = 0;
  $req = "SELECT SUM(lc.quantite) AS QCmd FROM ligne_commande AS lc, commande AS c WHERE c.GLN_Client_Liv=".$_SESSION['login']." AND c.Id_Commande=lc.Id_Commande AND (c.Date_fin_Liv BETWEEN '".$_POST['GestStock_DateD']."' AND '".$_POST['GestStock_DateF']."') AND lc.Id_colis='".$info['Id_colis']."'";
  $sql->query($req);
  if ($sql->num_rows() > 0)
  {
    $cmd = $sql->fetch_array();
  }
  else
   $cmd['QCmd'] = 0;
  $res = ($info['Quantite_Dispo'] + $cmd['QCmd']) - ($data['Quantite']);
  if ($res < $info['Seuil_Min'])
  {
   $_SESSION['commande'][$info['Id_colis']]['Quantite'] = $info['Seuil_Min'] - $res;
   $req = "SELECT GLN_FMP FROM distribution WHERE GLN_Entrepot='".$_SESSION['login']."' AND Id_colis='".$info['Id_colis']."'";
   $sql->query($req);
   $fmp = $sql->fetch_array();
   $_SESSION['commande'][$info['Id_colis']]['FMP'] = $fmp['GLN_FMP'];
   /* Start */ 
   $req = "SELECT lc.quantite AS Quantite, p.Date_Fin AS Date FROM ligne_commande AS lc, commande AS c, preparation AS p WHERE c.GLN_Entrepot=".$_SESSION['login']." AND c.Etat='p' AND c.Id_Commande=lc.Id_Commande AND c.Id_Commande=p.Id_Commande AND (p.Date_Fin BETWEEN '".$_POST['GestStock_DateD']."' AND'".$_POST['GestStock_DateF']."') AND lc.Id_colis='".$info['Id_colis']."' ORDER BY p.Date_fin";
   $sql->query($req);
   if ($sql->num_rows() > 0)
   {
    $date = array();
    while($tmp = $sql->fetch_array())
	{
     $date[] = $tmp;
	}
	$rez = ($info['Quantite_Dispo']); /* Gestion des cmd en cours a faire ... */
    foreach ($date AS $if => $k)
    {
     $rez -= $k['Quantite'];
	 if ($rez < $info['Seuil_Min'])
	 {
	  $_SESSION['commande'][$info['Id_colis']]['Date'] = $k['Date'];
	  break;
	 }
    }
   }
  }
 } 
}

if (isset($_POST) && isset($_GET['action']))
{
 if ($_GET['action'] == "view")
  view_all($_POST);
 else if ($_GET['action'] == "cmd")
  cmd_all($_POST);
}
?>